<?php
class CommonClass {
	public static function getAllDistrict($fields = '*') {
		$fields = strval ( trim ( $fields ) );
		return db_factory::get_table_data ( $fields, 'witkey_district', ' 1=1 ', 'displayorder asc', NULL, NULL, 'id', null );
	}
	public static function getDistrictName($upid) {
		$districtinfo= db_factory::get_one('select * from '.TABLEPRE.'witkey_district where id='.$upid);
		return $districtinfo['name'];
	}
	public static function getDistrictNames($upid) {
		$districtinfo= db_factory::get_one('select * from '.TABLEPRE.'witkey_district where id='.$upid);
		$districtinfos= db_factory::get_one('select * from '.TABLEPRE.'witkey_district where id='.$districtinfo['upid']);
		return $districtinfos['name'];
	}
	public static function getUpid($nameone) {
		$districtinfo= db_factory::get_one('select * from '.TABLEPRE.'witkey_district where name="'.$nameone.'"');
		if($districtinfo){
			$twoupid=$districtinfo['id'];
		}else{
			$insertsqlarr['name']=$nameone;
			$insertsqlarr['upid']="0";
			$intResult=db_factory::inserttable(TABLEPRE.'witkey_district', $insertsqlarr);
			$twoupid=$intResult;
		}
		return $twoupid;
	}
	public static function getDistrictById($id, $fields = '*') {
		$fields = strval ( trim ( $fields ) );
		return db_factory::get_one ( sprintf ( 'select %s from %s where id = %d', $fields, TABLEPRE . 'witkey_district', intval ( $id ) ) );
	}
	public static function getDistrictByPid($pid, $fields = '*') {
		$fields = strval ( trim ( $fields ) );
		return db_factory::get_table_data ( $fields, 'witkey_district', '1=1 and upid =' . intval ( $pid ), ' displayorder asc', NULL, NULL, 'id', NULL );
	}
	public static function getIndustryByPid($pid, $fields = '*') {
		return kekezu::get_table_data ( $fields, 'witkey_industry', 'indus_type =1 and indus_pid =' . intval ( $pid ), ' CASE WHEN listorder = 0 THEN 9999999 WHEN listorder > 0 THEN listorder END ', '', '', 'indus_id', 60 );
	}
	public static function getNearlyIncomeForDays($uid, $days = 30) {
		$sqlIncome = "SELECT SUM(fina_cash) FROM `" . TABLEPRE . "witkey_finance`
				WHERE uid = " . intval ( $uid ) . " AND (fina_action = 'task_bid' or fina_action = 'sale_service')
				AND fina_type = 'in' AND DATE_SUB(CURDATE(),INTERVAL 30 day) <= date(from_unixtime(fina_time))";
		return $nearlyIncome = number_format ( floatval ( db_factory::get_count ( $sqlIncome ) ), 2 );
	}
	public static function getFileArray($delimiter, $string) {
		$arrFileLists = array ();
		if ($string) {
			$arrFilePath = explode ( $delimiter, $string );
			if (is_array ( $arrFilePath )) {
				$strSql = sprintf ( "select file_id,file_name,save_name from %switkey_file where file_id in(%s)", TABLEPRE, implode ( ',', array_filter ( $arrFilePath ) ) );
				$arrFileLists = db_factory::query ( $strSql );
			}
		}
		return $arrFileLists;
	}
	public static function getFileArrayByPath($delimiter, $string) {
		$arrFileLists = array ();
		if ($string) {
			$arrFilePath = explode ( $delimiter, $string );
			if (is_array ( $arrFilePath )) {
				$arrFilePath = "'" . implode ( "','", array_filter ( $arrFilePath ) ) . "'";
				$strSql = sprintf ( "select file_id,file_name,save_name from %switkey_file where save_name in(%s)", TABLEPRE, $arrFilePath );
				$arrFileLists = db_factory::query ( $strSql );
			}
		}
		return $arrFileLists;
	}
	public static function delFileByFileId($fileId) {
		$strSql = sprintf ( "select file_id,file_name,save_name from %switkey_file where file_id in(%s)", TABLEPRE, intval ( $fileId ) );
		$arrFileInfo = db_factory::get_one ( $strSql );
		$filename = S_ROOT . $arrFileInfo ['save_name'];
		if (file_exists ( $filename )) {
			unlink ( $filename );
		}
		return db_factory::execute ( "delete from " . TABLEPRE . "witkey_file where file_id = " . intval ( $fileId ) );
	}
	public static function returnNewArr($val, $array) {
		$newArr = array ();
		foreach ( $array as $v ) {
			if ($v != $val) {
				$newArr [] = $v;
			}
		}
		return $newArr;
	}
	public static function getGoodsMark($intId) {
		$intGoodsMarks = db_factory::execute ( sprintf ( "select * from %switkey_mark where mark_type=2 and origin_id=%d", TABLEPRE, $intId ) );
		$intGoodsMark = db_factory::execute ( sprintf ( "select * from %switkey_mark where mark_type=2 and mark_status=1 and origin_id=%d", TABLEPRE, $intId ) );
		if (! empty ( $intGoodsMark ) && ! empty ( $intGoodsMarks )) {
			$floatMark = round ( $intGoodsMark / $intGoodsMarks, 4 ) * 100;
		} else {
			$floatMark = 0;
		}
		return $floatMark;
	}
	public static function getMemberFocus($intId) {
		$arrData = array ();
		$intFocusNum = db_factory::get_count ( "select count(*) from " . TABLEPRE . "witkey_free_follow  where uid=" . intval ( $intId ) );
		$intFensiNum = db_factory::get_count ( "select count(*) from " . TABLEPRE . "witkey_free_follow  where fuid=" . intval ( $intId ) );
		$arrData ['focusnum'] = $intFocusNum;
		$arrData ['fensinum'] = $intFensiNum;
		return $arrData;
	}
	public static function getShopInfo($intId) {
		return db_factory::get_one ( "select * from " . TABLEPRE . "witkey_shop where uid=" . intval ( $intId ) );
	}
	public static function getTradeRecord() {
		global $gUid;
		$arrTaskStatus = '3,4,5,6,7,8';
		$strSql = "select * from " . TABLEPRE . "witkey_task where task_status in (" . $arrTaskStatus . ") and uid=" . intval ( $gUid );
		$arrTaskInfo = db_factory::query ( $strSql );
		$strSql .= 'SELECT a.id,a.price,c.order_id,c.order_uid,c.order_username,c.seller_uid,c.seller_username,c.order_status,c.order_time FROM `' . TABLEPRE . 'witkey_service_order` as a LEFT JOIN ' . TABLEPRE . 'witkey_order_detail as b ON a.order_id = b.order_id LEFT JOIN ' . TABLEPRE . 'witkey_order as c ON a.order_id = c.order_id	WHERE 1=1 ' . " and c.order_status ='" . strval ( $s ) . "'";
		$arrServiceInfo = db_factory::query ( $strSql );
	}
	public static function getStatus($iTime) {
		$iTime = intval ( $iTime );
		$iCurrentTime = time ();
		$dur = $iCurrentTime - $iTime;
		if ($dur < 0) {
			return $iTime;
		} else {
			if ($dur < 60) {
				return '刚刚';
			} else {
				if ($dur < 3600) {
					return floor ( $dur / 60 ) . '分钟前';
				} else {
					if ($dur < 86400) {
						if (date ( 'd', $iCurrentTime ) == date ( 'd', $iTime )) {
							return date ( 'H:i', $iTime );
						} else {
							return '昨日 ' . date ( 'H:i', $iTime );
						}
					} else {
						if ($dur < 2592000) { 
							return floor ( $dur / 86400 ) . '天前';
						} else {
							return floor ( $dur / 2592000 ) . '月前';
						}
					}
				}
			}
		}
	}
	public static function getFooterPage($limit = 10){
		return db_factory::query("SELECT art_id,art_title,art_source FROM `".TABLEPRE."witkey_article` WHERE cat_type = 'about' ORDER BY listorder ASC,art_id DESC limit 0,".intval($limit));
	}
}
?>